home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / bin / ubuntuone-client-preferences < prev    next >
Text File  |  2009-10-29  |  14KB  |  358 lines

  1. #!/usr/bin/python
  2.  
  3. # ubuntuone-client-applet - Tray icon applet for managing Ubuntu One
  4. #
  5. # Author: Rodney Dawes <rodney.dawes@canonical.com>
  6. #
  7. # Copyright 2009 Canonical Ltd.
  8. #
  9. # This program is free software: you can redistribute it and/or modify it
  10. # under the terms of the GNU General Public License version 3, as published
  11. # by the Free Software Foundation.
  12. #
  13. # This program is distributed in the hope that it will be useful, but
  14. # WITHOUT ANY WARRANTY; without even the implied warranties of
  15. # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
  16. # PURPOSE.  See the GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program.  If not, see <http://www.gnu.org/licenses/>.
  20.  
  21. from __future__ import with_statement
  22.  
  23. import pygtk
  24. pygtk.require('2.0')
  25. import gobject
  26. import gtk
  27. import os
  28. import gettext
  29. from ubuntuone import clientdefs
  30.  
  31. import dbus.service
  32. from ConfigParser import ConfigParser
  33. from dbus.exceptions import DBusException
  34. from dbus.mainloop.glib import DBusGMainLoop
  35. from xdg.BaseDirectory import xdg_config_home
  36.  
  37. DBusGMainLoop(set_as_default=True)
  38.  
  39. _ = gettext.gettext
  40.  
  41. APPLET_IFACE_NAME = "com.ubuntuone.ClientApplet"
  42. APPLET_IFACE_CONFIG_NAME = APPLET_IFACE_NAME + ".Config"
  43.  
  44. DBUS_IFACE_NAME = "com.ubuntuone.SyncDaemon"
  45. DBUS_IFACE_CONFIG_NAME = DBUS_IFACE_NAME + ".Config"
  46.  
  47. # Why thank you GTK+ for enforcing style-set and breaking API
  48. RCSTYLE = """
  49. style 'dialogs' {
  50.   GtkDialog::action-area-border = 12
  51.   GtkDialog::button-spacing = 6
  52.   GtkDialog::content-area-border = 0
  53. }
  54. widget_class '*Dialog*' style 'dialogs'
  55. """
  56.  
  57. CONF_FILE = os.path.join(xdg_config_home, "ubuntuone", "ubuntuone-client.conf")
  58.  
  59. def dbus_async(*args):
  60.       """Simple handler to make dbus do stuff async."""
  61.       pass
  62.  
  63.  
  64. class AppletConfigDialog(gtk.Dialog):
  65.       """Preferences dialog."""
  66.  
  67.       def __init__(self, config=None, *args, **kw):
  68.             """Initializes our config dialog."""
  69.             super(AppletConfigDialog, self).__init__(*args, **kw)
  70.             self.set_title(_("Ubuntu One Preferences"))
  71.             self.set_has_separator(False)
  72.             self.add_button(gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE)
  73.             self.set_default_response(gtk.RESPONSE_CLOSE)
  74.             self.set_icon_name("ubuntuone-client")
  75.  
  76.             self.connect("close", self.__handle_response, gtk.RESPONSE_CLOSE)
  77.             self.connect("response", self.__handle_response)
  78.  
  79.             self.config = config
  80.  
  81.             self.bw_enabled = False
  82.             self.up_limit = 2097152
  83.             self.dn_limit = 2097152
  84.  
  85.             self.__bus = dbus.SessionBus()
  86.  
  87.             try:
  88.                   client = self.__bus.get_object(DBUS_IFACE_NAME, "/config",
  89.                                                  follow_name_owner_changes=True)
  90.                   iface = dbus.Interface(client, DBUS_IFACE_CONFIG_NAME)
  91.                   iface.get_throttling_limits(
  92.                         reply_handler=self.__got_limits,
  93.                         error_handler=self.__dbus_error)
  94.                   iface.bandwidth_throttling_enabled(
  95.                         reply_handler=self.__got_enabled,
  96.                         error_handler=self.__dbus_error)
  97.             except DBusException, e:
  98.                   self.__dbus_error(e)
  99.  
  100.             # Timeout ID to avoid spamming DBus from spinbutton changes
  101.             self.__update_id = 0
  102.  
  103.             self.load_config()
  104.  
  105.             self.__construct()
  106.  
  107.       def load_config(self):
  108.             """Load the configuration, and initilize if empty."""
  109.             if not os.path.isdir(os.path.dirname(CONF_FILE)):
  110.                   os.makedirs(os.path.dirname(CONF_FILE))
  111.  
  112.             self.config = ConfigParser()
  113.             self.config.read(CONF_FILE)
  114.  
  115.             if not self.config.has_section("ubuntuone"):
  116.                   self.config.add_section("ubuntuone")
  117.  
  118.             if not self.config.has_option("ubuntuone", "show_applet"):
  119.                   self.config.set("ubuntuone", "show_applet", "1")
  120.  
  121.             if not self.config.has_option("ubuntuone", "connect"):
  122.                   self.config.set("ubuntuone", "connect", "0")
  123.  
  124.             if not self.config.has_option("ubuntuone", "connected"):
  125.                   self.config.set("ubuntuone", "connected", "False")
  126.  
  127.             if not os.path.exists(CONF_FILE):
  128.                   self.save_config()
  129.  
  130.       def save_config(self):
  131.             """Write the configuration back to a file."""
  132.             with open(CONF_FILE, "w+b") as f:
  133.                   self.config.write(f)
  134.  
  135.       def quit(self):
  136.             """Exit the main loop."""
  137.             gtk.main_quit()
  138.  
  139.       def __dbus_error(self, error):
  140.             """Error getting throttling config."""
  141.             print repr(error)
  142.  
  143.       def __got_limits(self, limits):
  144.             """Got the throttling limits."""
  145.             self.up_limit = int(limits['upload'])
  146.             self.dn_limit = int(limits['download'])
  147.  
  148.       def __got_enabled(self, enabled):
  149.             """Got the throttling enabled config."""
  150.             self.bw_enabled = bool(enabled)
  151.  
  152.       def __update_bw_settings(self):
  153.             """Update the bandwidth throttling config in syncdaemon."""
  154.             self.bw_enabled = self.limit_check.get_active()
  155.             self.up_limit = self.up_spin.get_value_as_int() * 1024
  156.             self.dn_limit = self.dn_spin.get_value_as_int() * 1024
  157.  
  158.             try:
  159.                   client = self.__bus.get_object(DBUS_IFACE_NAME, "/config",
  160.                                                  follow_name_owner_changes=True)
  161.                   iface = dbus.Interface(client, DBUS_IFACE_CONFIG_NAME)
  162.                   iface.set_throttling_limits(self.dn_limit, self.up_limit,
  163.                         reply_handler=dbus_async,
  164.                         error_handler=self.__dbus_error)
  165.                   if self.bw_enabled:
  166.                         iface.enable_bandwidth_throttling(
  167.                               reply_handler=dbus_async,
  168.                               error_handler=self.__dbus_error)
  169.                   else:
  170.                         iface.disable_bandwidth_throttling(
  171.                               reply_handler=dbus_async,
  172.                               error_handler=self.__dbus_error)
  173.             except DBusException, e:
  174.                   self.__dbus_error(e)
  175.  
  176.       def __handle_response(self, dialog, response):
  177.             """Handle the dialog's response."""
  178.             self.hide()
  179.             self.config.set("ubuntuone", "show_applet",
  180.                             self.show_menu.get_active())
  181.             self.config.set("ubuntuone", "connect",
  182.                             self.conn_menu.get_active())
  183.  
  184.             self.__update_bw_settings()
  185.             self.save_config()
  186.             gtk.main_quit()
  187.  
  188.       def __show_menu_changed(self, menu, data=None):
  189.             """Update the config for showing the applet."""
  190.             value = menu.get_active()
  191.             self.config.set("ubuntuone", "show_applet", str(value))
  192.             try:
  193.                   client = self.__bus.get_object(APPLET_IFACE_NAME, "/config",
  194.                                                  follow_name_owner_changes=True)
  195.                   iface = dbus.Interface(client, APPLET_IFACE_CONFIG_NAME)
  196.                   iface.set_visibility_config(value, reply_handler=dbus_async,
  197.                                               error_handler=self.__dbus_error)
  198.             except DBusException, e:
  199.                   self.__dbus_error(e)
  200.  
  201.       def __conn_menu_changed(self, menu, data=None):
  202.             """Update the config for showing the applet."""
  203.             value = menu.get_active()
  204.             self.config.set("ubuntuone", "connect", str(value))
  205.             try:
  206.                   client = self.__bus.get_object(APPLET_IFACE_NAME, "/config",
  207.                                                  follow_name_owner_changes=True)
  208.                   iface = dbus.Interface(client, APPLET_IFACE_CONFIG_NAME)
  209.                   iface.set_connection_config(value, reply_handler=dbus_async,
  210.                                               error_handler=self.__dbus_error)
  211.             except DBusException, e:
  212.                   self.__dbus_error(e)
  213.  
  214.       def __bw_limit_toggled(self, button, data=None):
  215.             """Toggle the bw limit panel."""
  216.             self.bw_enabled = self.limit_check.get_active()
  217.             self.bw_table.set_sensitive(self.bw_enabled)
  218.             try:
  219.                   client = self.__bus.get_object(DBUS_IFACE_NAME, "/config",
  220.                                                  follow_name_owner_changes=True)
  221.                   iface = dbus.Interface(client, DBUS_IFACE_CONFIG_NAME)
  222.                   iface.set_throttling_limits(self.dn_limit, self.up_limit,
  223.                         reply_handler=dbus_async,
  224.                         error_handler=self.__dbus_error)
  225.                   if self.bw_enabled:
  226.                         iface.enable_bandwidth_throttling(
  227.                               reply_handler=dbus_async,
  228.                               error_handler=self.__dbus_error)
  229.                   else:
  230.                         iface.disable_bandwidth_throttling(
  231.                               reply_handler=dbus_async,
  232.                               error_handler=self.__dbus_error)
  233.             except DBusException, e:
  234.                   self.__dbus_error(e)
  235.  
  236.       def __spinner_changed(self, button, data=None):
  237.             """Remove timeout and add anew."""
  238.             if self.__update_id != 0:
  239.                   gobject.source_remove(self.__update_id)
  240.  
  241.             self.__update_id = gobject.timeout_add_seconds(
  242.                   1, self.__update_bw_settings)
  243.  
  244.       def __construct(self):
  245.             """Construct the dialog's layout."""
  246.             area = self.get_content_area()
  247.  
  248.             vbox = gtk.VBox(spacing=12)
  249.             vbox.set_border_width(12)
  250.             area.add(vbox)
  251.             vbox.show()
  252.  
  253.             # Put the first set of options in a table.
  254.             table = gtk.Table(rows=2, columns=2)
  255.             table.set_row_spacings(12)
  256.             table.set_col_spacings(6)
  257.             vbox.add(table)
  258.             table.show()
  259.  
  260.             label = gtk.Label(_("_Show icon:"))
  261.             label.set_use_underline(True)
  262.             label.set_alignment(0, 0.5)
  263.             table.attach(label, 0, 1, 0, 1)
  264.             label.show()
  265.  
  266.             self.show_menu = gtk.combo_box_new_text()
  267.             self.show_menu.connect("changed", self.__show_menu_changed)
  268.             label.set_mnemonic_widget(self.show_menu)
  269.             self.show_menu.append_text(_("Always"))
  270.             self.show_menu.append_text(_("When updating"))
  271.             self.show_menu.append_text(_("Never"))
  272.             self.show_menu.set_active(self.config.getint("ubuntuone",
  273.                                                          "show_applet"))
  274.             table.attach(self.show_menu, 1, 2, 0, 1)
  275.             self.show_menu.show()
  276.  
  277.             label = gtk.Label(_("Connect on start:"))
  278.             label.set_use_underline(True)
  279.             label.set_alignment(0, 0.5)
  280.             table.attach(label, 0, 1, 1, 2)
  281.             label.show()
  282.  
  283.             self.conn_menu = gtk.combo_box_new_text()
  284.             self.conn_menu.connect("changed", self.__conn_menu_changed)
  285.             label.set_mnemonic_widget(self.conn_menu)
  286.             self.conn_menu.append_text(_("Automatically"))
  287.             self.conn_menu.append_text(_("Remember last"))
  288.             self.conn_menu.append_text(_("Never"))
  289.             self.conn_menu.set_active(self.config.getint("ubuntuone",
  290.                                                          "connect"))
  291.             table.attach(self.conn_menu, 1, 2, 1, 2)
  292.             self.conn_menu.show()
  293.  
  294.             # Bandwidth limiting
  295.             self.limit_check = gtk.CheckButton(_("_Limit Bandwidth Usage"))
  296.             self.limit_check.connect("toggled", self.__bw_limit_toggled)
  297.             vbox.add(self.limit_check)
  298.             self.limit_check.show()
  299.  
  300.             hbox = gtk.HBox(spacing=12)
  301.             vbox.add(hbox)
  302.             hbox.show()
  303.  
  304.             label = gtk.Label()
  305.             hbox.add(label)
  306.             label.show()
  307.  
  308.             # Now put the bw limit bits in a table too
  309.             self.bw_table = gtk.Table(rows=2, columns=2)
  310.             self.bw_table.set_row_spacings(6)
  311.             self.bw_table.set_col_spacings(6)
  312.             self.bw_table.set_sensitive(False)
  313.             hbox.add(self.bw_table)
  314.             self.bw_table.show()
  315.  
  316.             # Upload speed
  317.             label = gtk.Label(_("Maximum _upload speed (KB/s):"))
  318.             label.set_use_underline(True)
  319.             label.set_alignment(0, 0.5)
  320.             self.bw_table.attach(label, 0, 1, 0, 1)
  321.             label.show()
  322.  
  323.             adjustment = gtk.Adjustment(value=2048.0, lower=0.0, upper=4096.0,
  324.                                         step_incr=64.0, page_incr=128.0)
  325.             self.up_spin = gtk.SpinButton(adjustment)
  326.             self.up_spin.connect("value-changed", self.__spinner_changed)
  327.             label.set_mnemonic_widget(self.up_spin)
  328.             self.bw_table.attach(self.up_spin, 1, 2, 0, 1)
  329.             self.up_spin.show()
  330.  
  331.             # Download speed
  332.             label = gtk.Label(_("Maximum _download speed (KB/s):"))
  333.             label.set_use_underline(True)
  334.             label.set_alignment(0, 0.5)
  335.             self.bw_table.attach(label, 0, 1, 1, 2)
  336.             label.show()
  337.             adjustment = gtk.Adjustment(value=2048.0, lower=0.0, upper=4096.0,
  338.                                         step_incr=64.0, page_incr=128.0)
  339.             self.dn_spin = gtk.SpinButton(adjustment)
  340.             self.dn_spin.connect("value-changed", self.__spinner_changed)
  341.             label.set_mnemonic_widget(self.dn_spin)
  342.             self.bw_table.attach(self.dn_spin, 1, 2, 1, 2)
  343.             self.dn_spin.show()
  344.  
  345.  
  346. if __name__ == "__main__":
  347.       gettext.bindtextdomain(clientdefs.GETTEXT_PACKAGE, clientdefs.LOCALEDIR)
  348.       gettext.textdomain(clientdefs.GETTEXT_PACKAGE)
  349.  
  350.       gtk.rc_parse_string(RCSTYLE)
  351.  
  352.       try:
  353.             dialog = AppletConfigDialog()
  354.             dialog.show()
  355.             gtk.main()
  356.       except KeyboardInterrupt:
  357.             pass
  358.